package com.apes.fn.fico.reimbursement.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.WorkPost;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 借支还款单
 * Created by lishikun on 2021/1/29 19:15
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DebitRepayment.CONST_TABLE_NAME)
public class DebitRepayment extends Model {
        public static final String CONST_TABLE_NAME = "fn_debit_repayment";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "借支还款单", padding = 5, prefix = "FH%(company)%(yy)%(month)", useDateRange=true)
    @Comment("借支还款编码")
    private String id;

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "post_id", referencedColumnName = "id")
    @Comment("岗位")
    private WorkPost workPost;

    @Comment("费用借支明细")
    @OneToMany(mappedBy = "debitRepayment", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<DebitRepaymentItem> debitRepaymentItems = new ArrayList<>();

    public List<DebitRepaymentItem> getDebitRepaymentItems() {
        for (DebitRepaymentItem item : debitRepaymentItems) {
            item.setDebitRepayment(this);
        }
        return debitRepaymentItems;
    }

    @States(name = "订单状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "approve", name = "审核"),
            @State(id = "finish", name = "完成"),
            @State(id = "repeal", name = "终止")
    })
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @Comment("备注")
    @Column(name = "remark", length = 120)
    private String remark;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveId;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    @Comment("控制父类审批")
    @Column(name = "need_Supper_OA")
    private String needSupperOA;

    @Comment("各类费用主管")
    @Column(name = "candidates", length = 300)
    private String candidates;




    @Comment("银行流水明细")
    @OneToMany(mappedBy = "debitRepayment", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<TransactionFlowItem> transactionFlowItems = new ArrayList<>();

    public List<TransactionFlowItem> getTransactionFlowItems() {
        for (TransactionFlowItem item : transactionFlowItems) {
            item.setDebitRepayment(this);
        }
        return transactionFlowItems;
    }
}
